package com.baymax.test.scene.demo

import android.animation.ObjectAnimator
import android.animation.PropertyValuesHolder
import android.animation.TypeEvaluator
import android.os.Bundle
import android.util.Property
import android.util.Size
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.setPadding
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.Player
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.newSceneFlexContainer
import com.bytedance.scene.Scene
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.http4k.lens.StringBiDiMappings.int

class GoneSurfaceScene : BaseScene() {
    val URL =
        "http://ad.vod01.icntvcdn.com/media/new/2013/icntv2/media/2019/05/14/H142108PhilipsOHCWilliamOutputProdu15sTM.ts"

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newSceneFlexContainer {
            setPadding(30)
            val ctx = requireSceneContext()
            val player1 = Player(ctx)
            val player2 = Player(ctx)
            val container = FrameLayout(ctx).apply {
                tag = player2
            }
            addDFView(150, 150, container) {
                addView(player1, ViewGroup.LayoutParams(-1, -1))
                player1.start(URL)
            }
            val container2 = FrameLayout(ctx).apply {
                tag = player2
            }
            container2.visibility = View.GONE
            addDFView(180, 180, container2) {

                addView(player2, ViewGroup.LayoutParams(-1, -1))
                player2.start()
            }
            addButton("animation") {
                player1.pivotX = 0f
                player1.pivotY = 0f
                val p1 = PropertyValuesHolder.ofObject<Size>(
                    SizeProp(),
                    object : TypeEvaluator<Size>{

                        override fun evaluate(
                            fraction: Float,
                            startValue: Size,
                            endValue: Size
                        ): Size {
                            val  w = startValue.width +  ((endValue.width - startValue.width) * fraction);
                            val h = startValue.height +  ((endValue.height - startValue.height) * fraction);
                            return Size(w.toInt(), h.toInt())

                        }

                    },
                    Size(100, 100),
                    Size(1140, 810)
                )
                ObjectAnimator.ofPropertyValuesHolder(player1,p1)
                    .setDuration(3000)
                    .start()
            }
            addButton("texture") {
                player1.switchTexture()
                player1.start(URL)
            }
            addButton("surface") {
                player1.switchSurface()
                player1.start(URL)
            }
            addButton("v1 gone/show") {
                println("delay show container 2")
                container.trigger()

            }
            addButton("v2 gone/show") {
                println("delay show container 2")
                container2.trigger()
            }
        }
    }

    fun ViewGroup.trigger() {
        this.visibility = when (this.visibility) {
            View.GONE, View.INVISIBLE -> {
                (this.tag as? Player)?.start()
                View.VISIBLE
            }

            else -> {
                (this.tag as? Player)?.stop()
                View.GONE
            }
        }
    }

    class SizeProp : Property<View, Size>(Size::class.java, "size") {

        var s = Size(0,0)


        override fun get(`object`: View): Size {
            s = Size(`object`.width, `object`.height)
            return s
        }

        override fun set(`object`: View, value: Size) {
            `object`.layoutParams = `object`.layoutParams.apply {
                width = value.width
                height = value.height

            }
        }
    }
}